<? 
require("adm_header.php"); 
//require("language_src/eng_system.inc");

writeCommonAdminTable(LA_ADMIN_ORDERS,1);
$lLimit=12;
$lStop=0;

if (getParam("validated","")!=2)
{
	echo "<form method='post' action='adm_orders.php'>";
	echo "&nbsp;<input type='text' name='find' value='".getParam("find","")."'>";
	echo "<input type='checkbox' name='o_status' value='Paid'";
	if (getParam("o_status","")=="Paid")
		echo " checked";	
	echo "> " . LA_ADMIN_ORDERS_PAID_ONLY;
	
	echo "&nbsp;&nbsp;<input type='submit' value='".LA_ADMIN_ORDERS_GET."' /></form>";
}



if (getParam("mark_paid",""))
{
	addPackage(getParam("user",""),getParam("addPid",""),getParam("mark_paid",""));	
	//$oid=makeOrder(getParam("addPid",""),getParam("user",""),time(),getParam("addPrice",""));
	completeOrder(getParam("mark_paid",""),"Paid",999,getParam("addPrice",""),getParam("addPid",""));

	echo "<p>".LA_ADMIN_ORDERS_COMPLETED."</p>";	
}
	
	
if (getParam("delete",""))
{
	$sql="delete from $pay_tbl where o_id=" . getParam("delete","");
	$r=q($sql);
	echo "<p>".LA_ADMIN_ORDERS_DELETED."</p>";	
}

// Find total
$lFind=getParam("find","");
$lSearchLimiter="";

if (getParam("o_id",""))
	$lSearchLimiter.=" AND o_id=" . getParam("o_id","");
	
if (getParam("o_status",""))
	$lSearchLimiter.=" AND o_status='" . getParam("o_status","") . "'";
	
	
if (getParam("find",""))
	$lSearchLimiter.=" AND (o_id='%$lFind%' OR o_date like '%$lFind%' OR o_user like '%$lFind%' OR o_package='%$lFind%')";	

if (getParam("user_id",""))
	$lSearchLimiter.=" AND (o_user = '".getparam("user_id","")."')";

if (getParam("validated","")==2)
	$lSearchLimiter.=" AND (ad_is_validated IS NULL)";	
	
// Get total count
$sql_select = "select count(o_id) as num_orders from $pay_tbl where 2>1 $lSearchLimiter"; 
$num_res=q($sql_select);
$lNumAds_row = mysql_fetch_array($num_res);
$lNumAds=$lNumAds_row["num_orders"];

$lUrl="&find=".getParam("find","") . "&o_id=" . getParam("o_id","") . "&orderby=" . getParam("orderby","o_id") . "&orderway=" . getParam("orderway","desc") . "&o_user=" . getParam("o_user","") . "&validated=" . getParam("validated","") . "&o_status=" . getParam("o_status","");

$lUrlNotOrder="&find=".getParam("find","") . "&o_id=" . getParam("o_id","") . "&o_user=" . getParam("o_user","") . "&validated=" . getParam("validated","")  . "&o_status=" . getParam("o_status","");


$retArray=urlBar($lNumAds,$lLimit,getParam("page",1),"$lUrl");
$sql_limit=$retArray["sql_limit"];

if ($lStop==0)
{
echo "<p>".LA_ADMIN_ORDERS_SEARCH_GAVE." <b>$lNumAds</b> ".LA_ADMIN_ORDERS_HITS."</p>";
$sql_select = "select * from $pay_tbl WHERE 1<2 $sql_part $lSearchLimiter order by " . getParam("orderby","o_id") . "  " .getParam("orderway","desc") . " $sql_limit";  
$result = q ($sql_select);

echo "<table border='0' cellpadding='2' cellspacing='0' width='100%' class='noBorder'>";
echo "<tr class='trHeadLine'>";
echo "<td><a class='navlink' href='?$lUrlNotOrder&orderby=o_id&orderway=desc'><u>".LA_ADMIN_ORDERS_ORDER."</u></a></td>";
echo "<td><a class='navlink' href='?$lUrlNotOrder&orderby=o_package&orderway=asc'>   <u>".LA_ADMIN_ORDERS_PACKAGE."</u></a></td>";
echo "<td><a class='navlink' href='?$lUrlNotOrder&orderby=o_user&orderway=desc'><u>".LA_ADMIN_ORDERS_USER."</u></a></td>";
echo "<td><a class='navlink' href='?$lUrlNotOrder&orderby=o_date&orderway=desc'><u>".LA_ADMIN_ORDERS_DATE_ORDERED."</u></a></td>";
echo "<td><a class='navlink' href='?$lUrlNotOrder&orderby=o_date_paid&orderway=desc'><u>".LA_ADMIN_ORDERS_DATE_PURCHASED."</u></a></td>";
echo "<td><a class='navlink' href='?$lUrlNotOrder&orderby=o_amount&orderway=desc'><u>".LA_ADMIN_ORDERS_AMOUNT."</u></a></td>";
echo "<td><b>Action</b></td>";
echo "</tr>";
$set_special=1;
while ($row = mysql_fetch_array($result))
{$sql = "select * from $pkg_tbl where pid=" . $row["o_package"];
	$res2=q($sql);
	$row_pkg = mysql_fetch_array($res2);
	$pkg_name=$row_pkg["pname"];	
	$color=colorSwitch($color);	
	echo "<tr class='$color'>";
	echo "<td>".$row["o_id"]."</td>";
	echo "<td>".$pkg_name."</td>";
	echo "<td><a href='adm_user.php?user_id=".$row["o_user"]."'>".$row["o_user"]."</a></td>";
	echo "<td>".formatDateShort($row["o_date"])."</td>";
	echo "<td>";
	if ($row["o_status"])
		echo " <font color='green'>";
	echo formatDateShort($row["o_date_paid"]);
	if ($row["o_status"])
		echo "</font>";
	echo "</td>";
	echo "<td>".$row["o_amount"]."</td>";	
	echo "<td>";
	echo "<table class='noBorder'><tr><td>";
	if (!$row["o_status"]=="Paid")
				echo "<a href='?$lUrl&o_id=".$row["o_id"]."&user=".$row["o_user"]."&mark_paid=".$row["o_id"]."&page=".getParam("page",1) . "&addPrice=".$row["o_amount"]."&addPid=".$row["o_package"]."'><img src='../layout_images/admin/check.gif' alt='Approve payment' border='0' /></a>";
	
				
	echo "</td><td><a href='?$lUrl&o_id=".getParam("o_id","")."&delete=".$row["o_id"]."&page=".getParam("page",1) . "'><img src='../layout_images/delete_round.gif' alt='Delete order' border='0' /></a>";
	echo "</td></tr></table></td>";	
	echo "</tr>";	
}
print("</table>");

echo ($retArray["url_bar"]); 
} // If $lStop==0;
writeCommonAdminTable("","");
require("adm_footer.php"); 
?>